/**
 * FileName: ProcuctTypeController
 * Author: Wb
 * Date: 2025/9/24 10:31
 * Description:
 * History:
 */
package org.test.device.controller;

import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.test.device.params.QueryProductTypeParam;
import org.test.device.params.SaveProductTypeParam;
import org.test.device.service.ProductTypeService;
import org.test.device.utils.ResultData;
import org.test.device.vo.product.ProductTypeVo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br>
 * 〈商品类型Controller〉
 *
 * @author Wb
 * @create 2025/9/24
 * @since 1.0.0
 */
@Slf4j
@Tag(name = "商品类型管理接口", description = "商品类型注册、查询相关接口")
@RestController
@RequestMapping("productType")
public class ProductTypeController {

    @Autowired
    ProductTypeService productTypeService;

    /**
     * @Author Wb
     * @Description 查询所有的商品类型
     * @Date 15:38 2025/9/24
     * @Return org.test.device.utils.ResultData
     */
    @GetMapping("list")
    public ResultData getProductTypeList() {
        List<Map<String,Object>> voList = productTypeService.getProductTypeList();
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("typeList", voList);
        return new ResultData("查询成功！", resultMap);
    }

    /**
     * @Author Wb
     * @Description 从sql里获取
     * @Date 9:12 2025/9/25
     * @Param []
     * @Return org.test.device.utils.ResultData
     */
    @GetMapping("sqlList")
    public ResultData getProductTypeListBySQL(@ParameterObject QueryProductTypeParam param) {
        log.info("前端查询参数param：{}", JSON.toJSONString(param));
        PageHelper.startPage(param.getCurrentPage(), param.getPageSize());
        List<ProductTypeVo> voList = productTypeService.getProductTypeListBySQL(param);
        PageInfo<ProductTypeVo> pageInfo = new PageInfo<>(voList);
        log.info("查询结果：{}，总数：{}，总页数：{}，当前页：{}", voList, pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum());
        return ResultData.pageResponse(voList, pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum());
    }

    /**
     * @Author Wb
     * @Description 新增商品类型
     * @Date 9:28 2025/9/25
     * @Param [SaveProductTypeParam]
     * @Return org.test.device.utils.ResultData
     */
    @PostMapping
    public ResultData saveProductType(@RequestBody SaveProductTypeParam param) {
        log.info("前端查询参数：{}", JSON.toJSONString(param));
        productTypeService.saveProductType(param);
        return new ResultData("添加成功！");
    }

    /**
     * @Author Wb
     * @Description 修改数据
     * @Date 11:25 2025/9/25
     * @Param [param]
     * @Return org.test.device.utils.ResultData
     */
    @PutMapping
    public ResultData updateProductType(@RequestParam("id") Integer id, @RequestBody SaveProductTypeParam param) {
        log.info("前端查询参数id：{}，param：{}", id,JSON.toJSONString(param));
        productTypeService.updateProductType(id, param);
        return new ResultData("修改成功！");
    }


    /**
     * @Author Wb
     * @Description 删除数据
     * @Date 10:36 2025/9/26
     * @Param [id]
     * @Return org.test.device.utils.ResultData
     */
    @DeleteMapping()
    public ResultData deletedProductType(@RequestParam("id")Integer id){
        log.info("前端查询参数id：{}", JSON.toJSONString(id));
        productTypeService.deletedProductType(id);
        return new ResultData("修改成功！");
    }
}